﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DockSample;

namespace DockSample.CodeGenerator
{
    class JavaEntityTemplate
    {
        private const string N = "\n";
        private const string T = "\t";
        private const string TEXT1 = "package com.zf.website.entity;" + N + N;
        private const string TEXT2 = "import java.util.Date;" + N;
        private const string TEXT3 = "import javax.persistence.Entity;" + N;
        private const string TEXT4 = "import javax.persistence.Temporal;" + N;
        private const string TEXT5 = "import javax.persistence.TemporalType;" + N;
        private const string TEXT6 = "import com.zf.generator.orm.hibernate.BaseEntity;" + N + N;
        private const string TEXT7 = "@Entity" + N;
        private const string TEXT8 = "public class ";
        private const string TEXT8_1 = " extends BaseEntity{" + N;

        private const string TEXT9 = T + "private ";
        private const string TEXT9_1 = ";" + N;

        private const string TEXT10 = T + "public ";
        private const string TEXT10_1 = T + "get";
        private const string TEXT10_2 = "(){" + N;
        private const string TEXT11 = T + T + "return ";
        private const string TEXT11_1 = ";" + N;
        private const string TEXT12 = T + "}" + N;

        private const string TEXT13 = T + "public void set";
        private const string TEXT13_1 = "(";
        private const string TEXT13_2 = ") {" + N;
        private const string TEXT14 = T + T + "this.";
        private const string TEXT14_1 = " = ";
        private const string TEXT14_2 = ";" + N;
        private const string TEXT15 = T + "}" + N;

        private const string TEXT16 = "}" + N;

        private const string TEXT_TEMPLATE_DATE = "@Temporal(TemporalType.DATE)" + N;

        public String generate(JavaEntityInfo entityInfo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(TEXT1);
            sb.Append(TEXT2);
            sb.Append(TEXT3);
            sb.Append(TEXT4);
            sb.Append(TEXT5);
            sb.Append(TEXT6);
            sb.Append(TEXT7);
            sb.Append(TEXT8);
            sb.Append(StringUtil.GetFirstLetterUpperCase(entityInfo.className));
            sb.Append(TEXT8_1);

            for (int i = 0; i < entityInfo.fieldList.Count; i++)
            {
                JavaEntityFieldInfo fieldInfo = entityInfo.fieldList[i];
                //写属性
                if (fieldInfo.fieldType.Equals(FieldType.Date.ToString()))
                {
                    sb.Append(TEXT_TEMPLATE_DATE);
                }
                sb.Append(TEXT9);
                sb.Append(fieldInfo.fieldType + " ");
                sb.Append(fieldInfo.fieldName);
                sb.Append(TEXT9_1);
                //写属性的getter
                sb.Append(TEXT10);
                sb.Append(fieldInfo.fieldType + " ");
                sb.Append(TEXT10_1);
                string fieldName = fieldInfo.fieldName.ToLower();
                string firstCap = StringUtil.GetFirstLetterUpperCase(fieldName);
                sb.Append(firstCap);
                sb.Append(TEXT10_2);
                sb.Append(TEXT11);
                sb.Append(fieldName);
                sb.Append(TEXT11_1);
                sb.Append(TEXT12);
                //写属性的setter
                sb.Append(TEXT13);
                sb.Append(firstCap);
                sb.Append(TEXT13_1);
                sb.Append(fieldInfo.fieldType + " ");
                sb.Append(fieldName);
                sb.Append(TEXT13_2);
                sb.Append(TEXT14);
                sb.Append(fieldName);
                sb.Append(TEXT14_1);
                sb.Append(fieldName);
                sb.Append(TEXT14_2);
                sb.Append(TEXT15);
            }
            sb.Append(TEXT16);
            return sb.ToString();
        }
    }
}
